package de.bht.netviz.ipsaver.lookup.dao;

import de.bht.netviz.dao.exceptions.NonexistentEntityException;
import de.bht.netviz.ipsaver.lookup.model.GeoPluginLookupData;
import de.bht.netviz.ipsaver.lookup.model.GeoPluginLookupData_;
import de.bht.netviz.ipsaver.lookup.model.ILookupData;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author mme2
 */
public class GeoPluginLookupDataJpaController implements Serializable, IJpaController {

   public GeoPluginLookupDataJpaController(EntityManagerFactory emf) {
      this.emf = emf;
   }
   private EntityManagerFactory emf = null;

   public EntityManager getEntityManager() {
      return emf.createEntityManager();
   }

   public void create(GeoPluginLookupData geoPluginLookupData) {
      EntityManager em = null;
      try {
         em = getEntityManager();
         em.getTransaction().begin();
         em.persist(geoPluginLookupData);
         em.getTransaction().commit();
      } finally {
         if (em != null) {
            em.close();
         }
      }
   }

   public void edit(GeoPluginLookupData geoPluginLookupData) throws NonexistentEntityException, Exception {
      EntityManager em = null;
      try {
         em = getEntityManager();
         em.getTransaction().begin();
         geoPluginLookupData = em.merge(geoPluginLookupData);
         em.getTransaction().commit();
      } catch (Exception ex) {
         String msg = ex.getLocalizedMessage();
         if (msg == null || msg.length() == 0) {
            long id = geoPluginLookupData.getId();
            if (findGeoPluginLookupData(id) == null) {
               throw new NonexistentEntityException("The geoPluginLookupData with id " + id + " no longer exists.");
            }
         }
         throw ex;
      } finally {
         if (em != null) {
            em.close();
         }
      }
   }

   public void destroy(long id) throws NonexistentEntityException {
      EntityManager em = null;
      try {
         em = getEntityManager();
         em.getTransaction().begin();
         GeoPluginLookupData geoPluginLookupData;
         try {
            geoPluginLookupData = em.getReference(GeoPluginLookupData.class, id);
            geoPluginLookupData.getId();
         } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The geoPluginLookupData with id " + id + " no longer exists.", enfe);
         }
         em.remove(geoPluginLookupData);
         em.getTransaction().commit();
      } finally {
         if (em != null) {
            em.close();
         }
      }
   }

   public List<GeoPluginLookupData> findGeoPluginLookupDataEntities() {
      return findGeoPluginLookupDataEntities(true, -1, -1);
   }

   public List<GeoPluginLookupData> findGeoPluginLookupDataEntities(int maxResults, int firstResult) {
      return findGeoPluginLookupDataEntities(false, maxResults, firstResult);
   }

   private List<GeoPluginLookupData> findGeoPluginLookupDataEntities(boolean all, int maxResults, int firstResult) {
      EntityManager em = getEntityManager();
      try {
         CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
         cq.select(cq.from(GeoPluginLookupData.class));
         Query q = em.createQuery(cq);
         if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
         }
         return q.getResultList();
      } finally {
         em.close();
      }
   }

   public GeoPluginLookupData findGeoPluginLookupData(long id) {
      EntityManager em = getEntityManager();
      try {
         return em.find(GeoPluginLookupData.class, id);
      } finally {
         em.close();
      }
   }

   public int getGeoPluginLookupDataCount() {
      EntityManager em = getEntityManager();
      try {
         CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
         Root<GeoPluginLookupData> rt = cq.from(GeoPluginLookupData.class);
         cq.select(em.getCriteriaBuilder().count(rt));
         Query q = em.createQuery(cq);
         return ((Long) q.getSingleResult()).intValue();
      } finally {
         em.close();
      }
   }

   GeoPluginLookupData findByIp(String ip) {
      GeoPluginLookupData retVal = null;
      
      
      CriteriaBuilder qb = this.emf.getCriteriaBuilder();
      CriteriaQuery<GeoPluginLookupData> c = qb.createQuery(GeoPluginLookupData.class);
      Root<GeoPluginLookupData> p = c.from(GeoPluginLookupData.class);
      Predicate condition = qb.equal(p.get(GeoPluginLookupData_.ip), ip);
      c.where(condition);
      TypedQuery<GeoPluginLookupData> q = this.emf.createEntityManager().createQuery(c); 
      List<GeoPluginLookupData> result = q.getResultList();
      
      if( result != null ) {
         switch( result.size() ) {
            case 1:
               retVal = result.get(0);
               break;
            case 0:
            default:
               retVal = null;
         }
      }
      return retVal;
   }
   
}
